<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加用户</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>
            <button data-target="./User.html" class="layui-btn layui-btn-sm" id="retur">返回
                <tton>

        </legend>

    </fieldset>

    <form class="layui-form" action="" onsubmit="return false">

        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-block">
                    <input type="text" name="mobile" placeholder="请输入手机号" lay-verify="required|phone" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="text" name="pwd" placeholder="请输入密码" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">管理员</label>
                <div class="layui-input-block">
                    <input type="checkbox" value="true" name="isAdmin" lay-skin="switch">
                    <div class="layui-unselect layui-form-switch" lay-skin="_switch">
                        <em></em>
                        <i></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter='add'>添加用户</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-1.12.4.min.js"></script>
    <script src="../js/common.js"></script>
    <script>

        layui.use(['form'], function () {
            const form = layui.form;

            //监听提交
            form.on('submit(add)', async function (data) {
                if (!data.field.isAdmin) {
                    data.field.isAdmin = false;
                }
                console.log(data.field)
                const res = await $.post("/api/user/zhuce", data.field);
                if (res) {
                    layer.msg('添加用户成功', {
                        icon: 1,
                        time: 1000
                    }, function () {
                        location.href = './User.html';
                    });
                } else {
                    layer.msg('手机号已存在！', {
                        icon: 2,
                        time: 1000
                    });
                }
            });
        });
        let retur = document.getElementById("retur");
        retur.onclick = function () {
            location.href = "./User.html";
        }

    </script>

</body>

<ml>